﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PowerSwitch.Properties;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace PowerSwitch
{
    public partial class Form1 : Form
    {
        private ToolStripMenuItem SelectedMenuItem;
        List<PowerPlanInfo> PowerPlans;
        PowerSettingNotificationMsgFilter pwrNotMsgFilter;

        public Form1()
        {
            InitializeComponent();
            notifyIcon.Icon = Resources.TypicalPowerIcon;
            this.Icon = Resources.TypicalPowerIcon;
            SelectedMenuItem = null;
        }

        // I have to override win proc as IMessageFilter was not filtering power messages
        protected override void WndProc(ref Message m)
        {
            if (pwrNotMsgFilter != null)
            {
                if (!pwrNotMsgFilter.PreFilterMessage(ref m))
                    base.WndProc(ref m);
            }
            base.WndProc(ref m);
        }

        #region Form
        private void Form1_Load(object sender, EventArgs e)
        {
            AddPowerPlansAsMenuItems();
            AddExitMenuItem();
            AttachPowerSettingNotificationMsgFilter();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            //hide immediatly
            Hide();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
                Hide();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DetachPowerSettingNotificationMsgFilter();
           
        }
        #endregion

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        #region PowerSettingNotification
        private void AttachPowerSettingNotificationMsgFilter()
        {
            pwrNotMsgFilter = new PowerSettingNotificationMsgFilter(this);
            pwrNotMsgFilter.OnPowerSchemeChanged += new Action<Guid>(pwrNotMsgFilter_OnPowerSchemeChanged);
        }

        private void DetachPowerSettingNotificationMsgFilter()
        {
            pwrNotMsgFilter.Destroy();
        }

        void pwrNotMsgFilter_OnPowerSchemeChanged(Guid newPersonality)
        {
            foreach (var p in PowerPlans)
            {
                if (p.SchemeGuid == newPersonality)
                {
                    CheckMenuItem((ToolStripMenuItem)p.Tag);
                    notifyIcon.Text = p.FriendlyName;

                    if (newPersonality == PowerSettingNotificationMsgFilter.GUID_MAX_POWER_SAVINGS)
                    {
                        notifyIcon.Icon = Resources.MaxSavingIcon;
                    }
                    else if (newPersonality == PowerSettingNotificationMsgFilter.GUID_MIN_POWER_SAVINGS)
                    {
                        notifyIcon.Icon = Resources.MaxPowerIcon;
                    }
                    else if (newPersonality == PowerSettingNotificationMsgFilter.GUID_TYPICAL_POWER_SAVINGS)
                    {
                        notifyIcon.Icon = Resources.TypicalPowerIcon;
                    }
                    else
                    {
                        notifyIcon.Icon = Resources.TypicalPowerIcon;
                    }
                }
            }
        }
        #endregion

        #region Menu Items
        void menuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            PowerPlanInfo powerPlanInfo = menuItem.Tag as PowerPlanInfo;

            powerPlanInfo.Set();
        }

        void menuExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddExitMenuItem()
        {
            ToolStripMenuItem menuExitItem = new ToolStripMenuItem("Exit");
            menuExitItem.Click += new EventHandler(menuExitItem_Click);
            this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { menuExitItem });
        }

        private void AddPowerPlansAsMenuItems()
        {
            PowerPlans = PowerSchemeHelper.GetAllPowerSchemas();
            foreach (var p in PowerPlans)
            {
                AddMenuItem(p);
            }
        }

        private void AddMenuItem(PowerPlanInfo powerPlanInfo)
        {
            ToolStripMenuItem menuItem = new ToolStripMenuItem(powerPlanInfo.FriendlyName);
            if (powerPlanInfo.IsActive)
            {
                CheckMenuItem(menuItem);
                notifyIcon.Text = powerPlanInfo.FriendlyName;
            }
            menuItem.Tag = powerPlanInfo;
            menuItem.Click += new EventHandler(menuItem_Click);
            powerPlanInfo.Tag = menuItem;
            this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { menuItem });
        }

        private void CheckMenuItem(ToolStripMenuItem menuItem)
        {
            //unchek previus one
            if (SelectedMenuItem != null)
                SelectedMenuItem.Checked = false;
            //remember wich is the current selected item
            SelectedMenuItem = menuItem;
            //applay the change
            menuItem.Checked = true;
        }     
        #endregion
    }
}
